<template>
	<view class="auth_rq">
		<image class="auth_rq_image" src="https://aliyunoss.hntgov.net/sd/ali_logo.png"></image>
		<view class="auth_rq_view">质惠游湖南</view>

		<view class="auth_rq_input" v-if="!isWx">
			<u-input v-model="value" :type="type" :border="border" :custom-style='customStyle' placeholder='请输入手机号' />
		</view>
		<view class="auth_rq_input" v-if="!isWx" style="margin-top: 50rpx;">
			<u-input v-model="value1" :type="type1" :border="border" :custom-style='customStyle' placeholder='请输入验证码' />
			<text style="margin-left: 30rpx;" :style="{color:isFlag?'#488CFE':'#666'}" @tap="getCode">{{tips}}</text>
		</view>
		
		<button class="auth_rq_button1" v-if="!isWx" style="margin-top: 80rpx;">立即登录</button>
		<button class="auth_rq_button2" v-if="!isWx" @click="isWx=true">手机号码快捷登录</button>
		<button class="auth_rq_button1" v-if="isWx"  type="primary" open-type="getAuthorize"  @getAuthorize="getPhoneSuc" 
		 @error="getPhoneErr" scope='phoneNumber'>手机号码快捷登录</button>
		  
		<!-- <button class="auth_rq_button2"  v-if="isWx"  @click="isWx=false">手机号码登录/注册</button> -->
		<view style="margin-top: 32rpx;margin-left: 70rpx;">
			<u-checkbox-group>
				<u-checkbox v-model="checked" shape="circle" style="color: #929292;">我已阅读并同意<text
						style="color: #488CFE;" @click="readXy()">用户服务协议</text></u-checkbox>
			</u-checkbox-group>
		</view>

		<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
			@change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	var that=''
	export default {
		data() {
			return {
				isWx:true,
				tips: '发送验证码',
				seconds: 60,
				checked: false,
				value: '',
				value1: '',
				type: 'text',
				type1: 'number',
				border: false,
				isFlag:true,
				customStyle: {
					background: '#f8f8f8',
					
				}
			}
		},
		onLoad() {
			that=this
			uni.removeStorageSync('isTo')
			this.login()
		},
		methods: {
			login(){
				uni.login({
					provider: 'alipay',
					success: function(loginRes) {
						that.$u.api.getCodeAuthUrl({
							code:loginRes.code
						}).then(res => {
							if (res) {
								uni.setStorageSync('openId',res.data.openId)
							}
						}) 
						
					}
				});
			},
			
			end() {
				this.isFlag=true
				
			},
			start() {
				this.isFlag=false
			},
			readXy() {
				uni.downloadFile({
				    url: "https://aliyunoss.hntgov.net/sd/zfzxy.docx",
				    success: function (res) {
				        let filePath = res.tempFilePath;   // 下载成功后返回的临时文件路径
				
				        // 打开临时文件
				        uni.openDocument({
				            filePath: filePath,
				            showMenu: true,
				            success: function (res) {
				                console.log('打开文档成功');
				            }
				        });
				    },
				    fail: (err) => {
				        // 处理下载失败的情况
				    }
				});
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						this.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
			getPhoneErr(err){
				console.log(1234);
				console.log(err);
			},
			getPhoneSuc() {
				if(!this.checked){
					uni.showToast({
						icon:'none',
						title:'请先阅读并同意用户服务协议'
					})
					return
				}
				uni.getPhoneNumber({
					success: (result) => {
						that.$u.api.getPhoneUrl({
							encryptContent:result.response,
							openId:uni.getStorageSync('openId')
						}).then(res => {
							if (res) {
								uni.removeStorageSync('isTo',1)
								uni.setStorageSync('isBind',1)
						        uni.setStorageSync('phone',res.data.mobile)
								uni.navigateBack()
							}
						}) 
					},
					fail: (res) => {
						console.log(res);
					},
				});
			}
		}
	}
</script>
<style>
	page {
		background: #ffffff;
	}
</style>
<style lang="less" scoped>
	.auth_rq {
		.auth_rq_input {
			display: flex;
			width: 610rpx;
			height: 98rpx;
			align-items: center;
			padding-left: 40rpx;
			margin-left: 70rpx;
			margin-top: 146rpx;
			background-color: #F8F8F8;
			border-radius: 8px;
			padding-right: 40rpx;
		}

		.auth_rq_image {
			width: 164rpx;
			height: 140rpx;
			margin-top: 182rpx;
			margin-left: 294rpx;
		}

		.auth_rq_view {
			width: 750rpx;
			text-align: center;
			color: #0DD381;
			font-size: 36rpx;
			margin-top: 18rpx;
		}

		.auth_rq_button1 {
			width: 610rpx;
			margin-left: 70rpx;
			margin-top: 284rpx;
			height: 98rpx;
			border-radius: 49rpx;
			background-color: #488CFE;
			color: #fff;
		}

		.auth_rq_button2 {
			width: 606rpx;
			margin-left: 70rpx;
			margin-top: 70rpx;
			height: 94rpx;
			border-radius: 49rpx;
			background-color: #fff;
			border: #488CFE 2rpx solid;
			color: #488CFE;
		}
	}
</style>